package com.bm.console.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.sys.object.common.utils.ExcelUtils;

public class xAction {
	static SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
	static Map<String, CellStyle> styleMap = new HashMap<String, CellStyle>(); // 存储单元格样式的Map
	/**
	 * 读excel
	 * 
	 * @param filePath
	 *            excel路径
	 */
	public static void readExcel(String filePath) {
		Workbook book = null;
		try {
			book = ExcelUtils.getExcelWorkbook(filePath);
			Sheet sheet = ExcelUtils.getSheetByNum(book, 1);
			System.out.println("sheet名称是：" + sheet.getSheetName());

			int lastRowNum = sheet.getLastRowNum();

			Row row = null;
			for (int i = 1; i <= lastRowNum; i++) {
				row = sheet.getRow(i);
				if (row != null) {
					System.out.println("正在读第" + (i + 1) + "行：");
					int lastCellNum = row.getLastCellNum();
					Cell cell = null;
					StringBuilder sb = null;
					for (int j = 0; j < lastCellNum; j++) {
						cell = row.getCell(j);
						if (cell != null) {
							sb = new StringBuilder("第" + (j + 1) + "列的单元格内容是：");
							String type_cn = null;
							String type_style = cell.getCellStyle().getDataFormatString().toUpperCase();
							String type_style_cn = ExcelUtils.getCellStyleByChinese(type_style);
							int type = cell.getCellType();
							String value = "";
							switch (type) {
							case 0:
								if (DateUtil.isCellDateFormatted(cell)) {
									type_cn = "NUMBER-DATE";
									Date date = cell.getDateCellValue();
									value = sFormat.format(date);
								} else {
									type_cn = "NUMBER";
									double tempValue = cell.getNumericCellValue();
									value = String.valueOf(tempValue);
								}
								break;
							case 1:
								type_cn = "STRING";
								value = cell.getStringCellValue();
								break;
							case 2:
								type_cn = "FORMULA";
								value = cell.getCellFormula();
								break;
							case 3:
								type_cn = "BLANK";
								value = cell.getStringCellValue();
								break;
							case 4:
								type_cn = "BOOLEAN";
								boolean tempValue = cell.getBooleanCellValue();
								value = String.valueOf(tempValue);
								break;
							case 5:
								type_cn = "ERROR";
								byte b = cell.getErrorCellValue();
								value = String.valueOf(b);
							default:
								break;
							}
							sb.append(value + ",内容类型是：" + type_cn + ",单元格的格式是：" + type_style_cn);
							System.out.println(sb.toString());
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
